﻿using System.Collections.Generic;
using LitJson;
using UnityEngine;

namespace CGF
{
    public static class GetPhoto
    {
        private static CTask<string> task;

        /// <summary>
        /// 拍摄一张照片并返回图片Base64
        /// </summary>
        public static async CTask<string> TakePhoto()
        {
            task = CTask<string>.Create();
            Debug.Log("功能未开发");
            return await task;
        }

        /// <summary>
        /// 从相册选择照片,返回图片路径
        /// </summary>
        public static async CTask<string[]> GetPhotoUrisFromAlbum()
        {
            bool allowMultiSelect = true;
            if (Application.isEditor)
            {
#if UNITY_EDITOR
                string path = UnityEditor.EditorUtility.OpenFilePanelWithFilters("选择照片", "", new string[] { "图片", "jpg,jpeg,png" });
                if (string.IsNullOrEmpty(path))
                {
                    return null;
                }
                return new[] { path };
#endif
                return null;
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                if (!GetPhotoAndroid.CheckAndRequestAlbumPermission())
                {
                    Debug.Log("未获得相册授权,尝试弹出授权申请");
                    return null;
                }
                string json = await GetPhotoAndroid._GetPhotoFromAlbum(allowMultiSelect);
                List<string> uris = new List<string>();
                JsonData jsonData = JsonMapper.ToObject(json);
                for (int i = 0; i < jsonData.Count; i++)
                {
                    uris.Add(jsonData[i].ToString());
                }
                return uris.ToArray();
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                string[] paths = await GetPhotoIOS.Instance._GetPhotoFromAlbum(allowMultiSelect ? 100 : 1);
                return paths;
            }
            else
            {
                string[] paths = await GetPhotoWeb.Instance._GetPhotoFromAlbum(allowMultiSelect);
                return paths;
            }
        }

        /// <summary>
        /// 从uri获取图片
        /// </summary>
        public static async CTask<Texture2D> GetPhotoByPath(string path, int maxSize = 800)
        {
            if (Application.isEditor)
            {
                byte[] bytes = System.IO.File.ReadAllBytes(path);
                return TextureHelper.Resize(TextureHelper.BytesToTexture(bytes), maxSize);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                return TextureHelper.Resize(await GetPhotoAndroid._GetTextureByUri(path), maxSize);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                byte[] bytes = GetPhotoIOS.Instance._GetBytesByPath(path);
                return TextureHelper.Resize(TextureHelper.BytesToTexture(bytes), maxSize);
            }
            else
            {
                //WebGL
                Texture2D tex = await GetPhotoWeb.Instance._GetTextureByPath(path);
                return TextureHelper.Resize(tex, maxSize);
            }
        }
    }
}
